//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <fstream>

#include <zlib.h>

#include "libraries.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHLINE 5000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool SplitColumn(char* buffer, TListE<String>* columns, const char* characters) {

  if (!buffer || !columns || !characters) return false;

  char *part = strtok(buffer, characters);

  while (part) {
    String* cell = new String(part);
    cell->Trim();
    columns->Add(cell);
    part = strtok(NULL, characters);
  }

  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main(int argc, char** argv) {

  String* filenameListIn;
  String* location;
  String* record;
  String* text;
  String* filenameOut;

  if (argc < 6) {
    cout << "Error in the input arguments. The execution must be: " << endl;
    cout << "./SearchGroupFiles <Input list file> <Location Database> <output file> <record> <search text>" << endl;
    return 0;
  }
  else {
    cout << "---------------------------------------------------------------------------------" << endl;
    cout << "  Input List File : " << argv[1] << endl;
    cout << "         Location : " << argv[2] << endl;
    cout << "      Output File : " << argv[3] << endl;
    cout << "       Record IDE : " << argv[4] << endl;
    cout << "      Search Text : " << argv[5] << endl;
    cout << "---------------------------------------------------------------------------------" << endl;
    filenameListIn = new String(argv[1]);
    location       = new String(argv[2]);
    filenameOut    = new String(argv[3]);
    record         = new String(argv[4]);
    text           = new String(argv[5]);
  }

  fstream          fileList, fileOut;
  char             buffer[LENGTHLINE + 1];
  TListE <String>* listFiles = new TListE <String> ();

  fileList.open(filenameListIn->Out(), fstream::in);
  fileOut.open(filenameOut->Out(), fstream::out);

  while (!fileList.eof()) {
    fileList.getline(buffer, LENGTHLINE);
    String* line = new String(buffer);
    line->RTrim();
    listFiles->Add(line);
  }
  fileList.close();

  record->Trim();
  text->Trim();
  text->ToLower();

  if (listFiles->Length()) {

    listFiles->SetInitial();

    while (listFiles->SetNext()) {

      char completePath[location->Length() + listFiles->GetCurrent()->Length() + 30];
      sprintf(completePath, "%s%s\0", location->Out(), listFiles->GetCurrent()->Out());

      cout << "File: " << completePath << endl;

      gzFile pdbFile   = gzopen(completePath, "rb9");

      while (!gzeof(pdbFile)) {

        gzgets(pdbFile, buffer, LENGTHLINE);

        if (!strncmp(buffer, record->Out(), record->Length())) {
          String line;
          line.In(buffer);
          line.ToLower();
          if (strstr(line.Out(), text->Out())) {
            fileOut << listFiles->GetCurrent()->Out() << endl;
            break;
          }
        }
        
        if (!strncmp(buffer, "DBREF", strlen("DBREF")))
          break;

      }

      gzclose(pdbFile);

    }

  }

  fileOut.close();

  if (listFiles)      delete listFiles;

  if (filenameListIn) delete filenameListIn;
  if (location)       delete location;
  if (filenameOut)    delete filenameOut;
  if (record)         delete record;
  if (text)           delete text;

  return 1;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//




